namespace IMPlant.Core.Web.Control.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class GridViewHelper
    {
        public static List<DataKey> GetCheckedDataKey(GridView gv, int columnIndex)
        {
            if (gv.DataKeyNames.Length == 0)
            {
                throw new ArgumentNullException("DataKeys", "未设置GridView的DataKeyNames");
            }
            List<DataKey> list = new List<DataKey>();
            int num = 0;
            foreach (GridViewRow row in gv.Rows)
            {
                if (row.RowType != DataControlRowType.DataRow)
                {
                    continue;
                }
                foreach (Control control in row.Cells[columnIndex].Controls)
                {
                    if ((control is CheckBox) && ((CheckBox) control).Checked)
                    {
                        list.Add(gv.DataKeys[num]);
                        break;
                    }
                }
                num++;
            }
            return list;
        }

        public static List<DataKey> GetCheckedDataKey(GridView gv, string checkboxId)
        {
            return GetCheckedDataKey(gv, GetColumnIndex(gv, checkboxId));
        }

        public static int GetColumnIndex(GridView gv, string controlId)
        {
            foreach (GridViewRow row in gv.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    foreach (Control control in row.Cells[i].Controls)
                    {
                        if (control.ID == controlId)
                        {
                            return i;
                        }
                    }
                }
            }
            return -1;
        }

        public static int GetGridViewColumnNum(int nColNum, string sColSpan)
        {
            string[] strArray = Common.ReplaceCommaChars(sColSpan).Split(new char[] { ';' });
            int length = strArray.Length;
            int num2 = nColNum;
            for (int i = 0; i < length; i++)
            {
                int num4;
                string str = strArray[i].ToString();
                if (!int.TryParse(str.Substring(str.IndexOf(",") + 1), out num4))
                {
                    num4 = 1;
                }
                num2 += num4 - 1;
            }
            return num2;
        }

        public static void MergeCells(GridView gv, int[] columnIndices)
        {
            int length = columnIndices.Length;
            int[] numArray = new int[length];
            bool[] flagArray = new bool[length];
            for (int i = 0; i < length; i++)
            {
                numArray[i] = 0;
            }
            for (int j = 0; j < length; j++)
            {
                flagArray[j] = true;
            }
            int count = gv.Rows.Count;
            for (int k = 1; k < count; k++)
            {
                if ((gv.Rows[k].RowType == DataControlRowType.DataRow) && (gv.Rows[k - 1].RowType == DataControlRowType.DataRow))
                {
                    for (int m = 0; m < length; m++)
                    {
                        if ((columnIndices[m] >= 0) && (columnIndices[m] <= (gv.Columns.Count - 1)))
                        {
                            bool flag = gv.Rows[k].Cells[columnIndices[m]].HasControls();
                            bool flag2 = false;
                            bool flag3 = false;
                            if (flag)
                            {
                                flag2 = flag && (gv.Rows[k].Cells[columnIndices[m]].Controls[1] is Label);
                                flag3 = flag && (gv.Rows[k].Cells[columnIndices[m]].Controls[1] is LinkButton);
                            }
                            if (flag2)
                            {
                                flag2 = ((Label) gv.Rows[k].Cells[columnIndices[m]].Controls[1]).Text == ((Label) gv.Rows[k - 1].Cells[columnIndices[m]].Controls[1]).Text;
                            }
                            if (flag3)
                            {
                                flag3 = ((LinkButton) gv.Rows[k].Cells[columnIndices[m]].Controls[1]).Text == ((LinkButton) gv.Rows[k - 1].Cells[columnIndices[m]].Controls[1]).Text;
                            }
                            flag2 = flag2 || flag3;
                            if (flag)
                            {
                                if (flag2)
                                {
                                    if (flagArray[m])
                                    {
                                        numArray[m] = k - 1;
                                    }
                                    if (gv.Rows[numArray[m]].Cells[columnIndices[m]].RowSpan == 0)
                                    {
                                        gv.Rows[numArray[m]].Cells[columnIndices[m]].RowSpan = 1;
                                    }
                                    TableCell cell1 = gv.Rows[numArray[m]].Cells[columnIndices[m]];
                                    cell1.RowSpan++;
                                    gv.Rows[k].Cells[columnIndices[m]].Visible = false;
                                    flagArray[m] = false;
                                }
                                else
                                {
                                    flagArray[m] = true;
                                }
                            }
                            else if (gv.Rows[k].Cells[columnIndices[m]].Text == gv.Rows[k - 1].Cells[columnIndices[m]].Text)
                            {
                                if (flagArray[m])
                                {
                                    numArray[m] = k - 1;
                                }
                                if (gv.Rows[numArray[m]].Cells[columnIndices[m]].RowSpan == 0)
                                {
                                    gv.Rows[numArray[m]].Cells[columnIndices[m]].RowSpan = 1;
                                }
                                TableCell cell2 = gv.Rows[numArray[m]].Cells[columnIndices[m]];
                                cell2.RowSpan++;
                                gv.Rows[k].Cells[columnIndices[m]].Visible = false;
                                flagArray[m] = false;
                            }
                            else
                            {
                                flagArray[m] = true;
                            }
                        }
                    }
                }
            }
        }
    }
}

